﻿using ERP.Inventory.Api.Read.Application.Command.库存管理;
using ERP.Inventory.Api.Read.DTO;
using ERP.Inventory.Domain.库存管理;
using ERP.Inventory.Errorcode;
using ERP.Inventory.Infastructure.Interface;
using MediatR;

namespace ERP.Inventory.Api.Read.Application.Handler.库存管理
{
    public class GetShopHandler : IRequestHandler<GetShopCommand, ApiPaging<ShopModel>>
    {
        private readonly IBaseRepository<ShopModel> shopRepo;

        public GetShopHandler(IBaseRepository<ShopModel> shopRepo)
        {
            this.shopRepo = shopRepo;
        }
        public Task<ApiPaging<ShopModel>> Handle(GetShopCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiPaging<ShopModel>();
            var shop = shopRepo.GetAll().Where(x => x.IsDel == false);

            var totalcount = shop.Count();
            var totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.PageSize);
            shop = shop.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            res.totalcount = totalcount;
            res.totalpage = totalpage;
            res.Message = "显示成功";
            res.Data = shop.ToList();
            res.Code = ApiEnum.Ok;
            return Task.FromResult(res);
        }
    }
}
